home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / WINPROGS.ARJ / SYSMETS3.C < prev    next >
Text File  |  1990-11-24  |  7KB  |  236 lines

  1. /*   sysmets3.C   -  System Metrics Display Program No. 3
  2.                      Petzold
  3. */
  4.  
  5. #include <windows.h>
  6. #include "sysmets.h"
  7.  
  8. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG);
  9.  
  10. int PASCAL WinMain (HANDLE hInstance,
  11.                     HANDLE hPrevInstance,
  12.                     LPSTR  lpszCmdParam,
  13.                     int    nCmdShow)
  14.                     
  15.   {
  16.   static char szAppName[] = "SysMets3";
  17.   HWND        hwnd;
  18.   MSG         msg;
  19.   WNDCLASS    wndclass;
  20.   
  21.   if (!hPrevInstance)
  22.      {
  23.      wndclass.style            = CS_HREDRAW | CS_VREDRAW;
  24.      wndclass.lpfnWndProc      = WndProc;
  25.      wndclass.cbClsExtra       = 0;
  26.      wndclass.cbWndExtra       = 0;
  27.      wndclass.hInstance        = hInstance;
  28.      wndclass.hIcon            = LoadIcon (NULL, IDI_APPLICATION);
  29.      wndclass.hCursor          = LoadCursor (NULL, IDC_ARROW);
  30.      wndclass.hbrBackground    = GetStockObject (WHITE_BRUSH);
  31.      wndclass.lpszMenuName     = NULL;
  32.      wndclass.lpszClassName    = szAppName;
  33.      
  34.      RegisterClass(&wndclass);
  35.      }
  36.      
  37.   hwnd = CreateWindow (szAppName,
  38.                        "Get System Metrics No. 3",
  39.                        WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, 
  40.                        CW_USEDEFAULT,   
  41.                        CW_USEDEFAULT,
  42.                        CW_USEDEFAULT,   
  43.                        CW_USEDEFAULT,   
  44.                        NULL,
  45.                        NULL,
  46.                        hInstance,
  47.                        NULL);
  48.                        
  49.   ShowWindow (hwnd, nCmdShow);
  50.   UpdateWindow (hwnd);
  51.   
  52.   while (GetMessage (&msg, NULL, 0, 0))
  53.     {
  54.     TranslateMessage (&msg);
  55.     DispatchMessage  (&msg);
  56.     }
  57.     
  58.   return msg.wParam;
  59.   }
  60.   
  61. long FAR PASCAL WndProc (HWND hwnd,
  62.                          WORD message,
  63.                          WORD wParam,
  64.                          LONG lParam)
  65.                          
  66.   {
  67.   static short cxChar, cxCaps, cyChar, cxClient, cyClient, 
  68.                nMaxWidth, nVscrollPos, nVscrollMax,
  69.                nHscrollPos, nHscrollMax;
  70.   char         szBuffer[10];
  71.   short        i, x, y, nPaintBeg, nPaintEnd, nVscrollInc, nHscrollInc;
  72.   TEXTMETRIC   tm;
  73.   HDC          hdc;
  74.   PAINTSTRUCT  ps;
  75.   
  76.   switch (message)
  77.     {
  78.     case WM_CREATE:
  79.       hdc = GetDC (hwnd);
  80.       
  81.       GetTextMetrics (hdc, &tm);
  82.       cxChar = tm.tmAveCharWidth;
  83.       cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar /2;
  84.       cyChar = tm.tmHeight + tm.tmExternalLeading;
  85.       
  86.       ReleaseDC (hwnd, hdc);
  87.       
  88.       nMaxWidth = 40 * cxChar + 18 * cxCaps;      
  89.       return 0;
  90.       
  91.     case WM_SIZE:
  92.       cyClient = HIWORD (lParam);
  93.       cxClient = LOWORD (lParam);
  94.  
  95.       nVscrollMax = max (0, NUMLINES + 2 - cyClient / cyChar);
  96.       nVscrollPos = min (nVscrollPos, nVscrollMax);
  97.       
  98.       SetScrollRange (hwnd, SB_VERT, 0, nVscrollMax, FALSE);
  99.       SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE);
  100.       
  101.       nHscrollMax = max (0, 2 + (nMaxWidth - cyClient) / cxChar);
  102.       nHscrollPos = min (nHscrollPos, nHscrollMax);
  103.       
  104.       SetScrollRange (hwnd, SB_HORZ, 0, nHscrollMax, FALSE);
  105.       SetScrollPos (hwnd, SB_HORZ, nHscrollPos, TRUE);
  106.       
  107.       return 0;
  108.       
  109.     case WM_VSCROLL:
  110.       switch (wParam)
  111.         {
  112.         case SB_TOP:
  113.           nVscrollInc = -nVscrollPos;
  114.           break;
  115.         
  116.         case SB_BOTTOM:
  117.           nVscrollInc = nVscrollMax - nVscrollPos;
  118.           break;
  119.             
  120.         case SB_LINEUP:
  121.           nVscrollInc = -1;
  122.           break;
  123.           
  124.         case SB_LINEDOWN:
  125.           nVscrollInc = 1;
  126.           break;
  127.           
  128.         case SB_PAGEUP:
  129.           nVscrollInc = min (-1, -cyClient / cyChar);
  130.           break;
  131.           
  132.         case SB_PAGEDOWN:
  133.           nVscrollInc = max (1, cyClient / cyChar);
  134.           break;
  135.           
  136.         case SB_THUMBPOSITION:
  137.           nVscrollInc = LOWORD (lParam) - nVscrollPos;
  138.           break;
  139.           
  140.         default:
  141.           nVscrollInc = 0;
  142.           break;
  143.         }
  144.  
  145.       if (nVscrollInc = max (-nVscrollPos,
  146.                              min (nVscrollInc, nVscrollMax - nVscrollPos)))
  147.                              
  148.         {
  149.         nVscrollPos += nVscrollInc;
  150.         ScrollWindow (hwnd, 0, -cyChar * nVscrollInc, NULL, NULL);
  151.         SetScrollPos (hwnd, SB_VERT, nVscrollPos, TRUE);
  152.         UpdateWindow (hwnd);
  153.         }
  154.       
  155.       return 0;
  156.     
  157.     case WM_HSCROLL:
  158.       switch (wParam)
  159.         {
  160.         case SB_LINEUP:
  161.           nHscrollInc = -1;
  162.           break;
  163.           
  164.         case SB_LINEDOWN:
  165.           nHscrollInc = 1;
  166.           break;
  167.           
  168.         case SB_PAGEUP:
  169.           nHscrollInc = -8;
  170.           break;
  171.           
  172.         case SB_PAGEDOWN:
  173.           nHscrollInc = 8;
  174.           break;
  175.           
  176.         case SB_THUMBPOSITION:
  177.           nHscrollInc = LOWORD (lParam) - nHscrollPos;
  178.           break;
  179.           
  180.         default:
  181.           nHscrollInc = 0;
  182.         }
  183.         
  184.         if (nHscrollInc = max (-nHscrollPos,
  185.                                min (nHscrollInc, nHscrollMax - nHscrollPos)))
  186.                     
  187.            {
  188.            nHscrollPos += nHscrollInc;
  189.            ScrollWindow (hwnd, -cxChar * nHscrollInc, 0, NULL, NULL);
  190.            SetScrollPos (hwnd, SB_HORZ, nHscrollPos, TRUE);
  191.            }
  192.          return 0;
  193.          
  194.                                 
  195.     case WM_PAINT:
  196.       hdc = BeginPaint (hwnd, &ps);
  197.  
  198.       nPaintBeg = max (0, nVscrollPos + ps.rcPaint.top / cyChar -1);
  199.       nPaintEnd = min (NUMLINES,
  200.                        nVscrollPos + ps.rcPaint.bottom / cyChar);
  201.                        
  202.       for (i = nPaintBeg; i < nPaintEnd; i++)
  203.         {
  204.         x = cxChar * (1 - nHscrollPos);
  205.         y = cyChar * ( 1 - nVscrollPos + i);
  206.         
  207.         TextOut (hdc, x, y,
  208.                  sysmetrics[i].szLabel,
  209.                  lstrlen (sysmetrics[i].szLabel));
  210.  
  211.         TextOut (hdc, x + 18 * cxCaps, y,
  212.                  sysmetrics[i].szDesc,
  213.                  lstrlen (sysmetrics[i].szDesc));
  214.                  
  215.          SetTextAlign (hdc, TA_RIGHT | TA_TOP);
  216.          
  217.          TextOut (hdc, x + 18 * cxCaps + 40 * cxChar, y,
  218.                   szBuffer,
  219.                   wsprintf(szBuffer, "%5d",
  220.                            GetSystemMetrics (sysmetrics[i].nIndex)));
  221.                            
  222.          SetTextAlign (hdc, TA_LEFT | TA_TOP);                           
  223.          }
  224.          
  225.        EndPaint (hwnd, &ps);
  226.        return 0;
  227.          
  228.     case WM_DESTROY:
  229.       PostQuitMessage (0);
  230.       return 0;
  231.     }
  232.     
  233.   return DefWindowProc (hwnd, message, wParam, lParam);
  234.   }
  235.                   
  236.